﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace Maticsoft.DAL
{
	/// <summary>
	/// 数据访问类:Orders
	/// </summary>
	public partial class Orders
	{
		public Orders()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("OrderId", "Orders"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int OrderId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Orders");
			strSql.Append(" where OrderId=@OrderId");
			SqlParameter[] parameters = {
					new SqlParameter("@OrderId", SqlDbType.Int,4)
			};
			parameters[0].Value = OrderId;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Maticsoft.Model.Orders model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Orders(");
			strSql.Append("UserId,Total,OitemNum,Name,sex,Phone,Mobile,Province,City,ProvinceName,CityName,Address,PostCode,CreateTime,UpdateTime,IsActive,Status,PayTime,PayWayId,DeliveryWayId,DeliveryFee,CouponFee,ProductPrice,DeliveryTimeId,trade_no)");
			strSql.Append(" values (");
			strSql.Append("@UserId,@Total,@OitemNum,@Name,@sex,@Phone,@Mobile,@Province,@City,@ProvinceName,@CityName,@Address,@PostCode,@CreateTime,@UpdateTime,@IsActive,@Status,@PayTime,@PayWayId,@DeliveryWayId,@DeliveryFee,@CouponFee,@ProductPrice,@DeliveryTimeId,@trade_no)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@Total", SqlDbType.Money,8),
					new SqlParameter("@OitemNum", SqlDbType.Int,4),
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@sex", SqlDbType.Int,4),
					new SqlParameter("@Phone", SqlDbType.NVarChar,50),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,50),
					new SqlParameter("@Province", SqlDbType.Int,4),
					new SqlParameter("@City", SqlDbType.Int,4),
					new SqlParameter("@ProvinceName", SqlDbType.NVarChar,20),
					new SqlParameter("@CityName", SqlDbType.NVarChar,20),
					new SqlParameter("@Address", SqlDbType.NVarChar,200),
					new SqlParameter("@PostCode", SqlDbType.NVarChar,50),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@UpdateTime", SqlDbType.DateTime),
					new SqlParameter("@IsActive", SqlDbType.Int,4),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@PayTime", SqlDbType.DateTime),
					new SqlParameter("@PayWayId", SqlDbType.Int,4),
					new SqlParameter("@DeliveryWayId", SqlDbType.Int,4),
					new SqlParameter("@DeliveryFee", SqlDbType.Money,8),
					new SqlParameter("@CouponFee", SqlDbType.Money,8),
					new SqlParameter("@ProductPrice", SqlDbType.Money,8),
					new SqlParameter("@DeliveryTimeId", SqlDbType.Int,4),
					new SqlParameter("@trade_no", SqlDbType.NVarChar,50)};
			parameters[0].Value = model.UserId;
			parameters[1].Value = model.Total;
			parameters[2].Value = model.OitemNum;
			parameters[3].Value = model.Name;
			parameters[4].Value = model.sex;
			parameters[5].Value = model.Phone;
			parameters[6].Value = model.Mobile;
			parameters[7].Value = model.Province;
			parameters[8].Value = model.City;
			parameters[9].Value = model.ProvinceName;
			parameters[10].Value = model.CityName;
			parameters[11].Value = model.Address;
			parameters[12].Value = model.PostCode;
			parameters[13].Value = model.CreateTime;
			parameters[14].Value = model.UpdateTime;
			parameters[15].Value = model.IsActive;
			parameters[16].Value = model.Status;
			parameters[17].Value = model.PayTime;
			parameters[18].Value = model.PayWayId;
			parameters[19].Value = model.DeliveryWayId;
			parameters[20].Value = model.DeliveryFee;
			parameters[21].Value = model.CouponFee;
			parameters[22].Value = model.ProductPrice;
			parameters[23].Value = model.DeliveryTimeId;
			parameters[24].Value = model.trade_no;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Maticsoft.Model.Orders model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Orders set ");
			strSql.Append("UserId=@UserId,");
			strSql.Append("Total=@Total,");
			strSql.Append("OitemNum=@OitemNum,");
			strSql.Append("Name=@Name,");
			strSql.Append("sex=@sex,");
			strSql.Append("Phone=@Phone,");
			strSql.Append("Mobile=@Mobile,");
			strSql.Append("Province=@Province,");
			strSql.Append("City=@City,");
			strSql.Append("ProvinceName=@ProvinceName,");
			strSql.Append("CityName=@CityName,");
			strSql.Append("Address=@Address,");
			strSql.Append("PostCode=@PostCode,");
			strSql.Append("CreateTime=@CreateTime,");
			strSql.Append("UpdateTime=@UpdateTime,");
			strSql.Append("IsActive=@IsActive,");
			strSql.Append("Status=@Status,");
			strSql.Append("PayTime=@PayTime,");
			strSql.Append("PayWayId=@PayWayId,");
			strSql.Append("DeliveryWayId=@DeliveryWayId,");
			strSql.Append("DeliveryFee=@DeliveryFee,");
			strSql.Append("CouponFee=@CouponFee,");
			strSql.Append("ProductPrice=@ProductPrice,");
			strSql.Append("DeliveryTimeId=@DeliveryTimeId,");
			strSql.Append("trade_no=@trade_no");
			strSql.Append(" where OrderId=@OrderId");
			SqlParameter[] parameters = {
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@Total", SqlDbType.Money,8),
					new SqlParameter("@OitemNum", SqlDbType.Int,4),
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@sex", SqlDbType.Int,4),
					new SqlParameter("@Phone", SqlDbType.NVarChar,50),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,50),
					new SqlParameter("@Province", SqlDbType.Int,4),
					new SqlParameter("@City", SqlDbType.Int,4),
					new SqlParameter("@ProvinceName", SqlDbType.NVarChar,20),
					new SqlParameter("@CityName", SqlDbType.NVarChar,20),
					new SqlParameter("@Address", SqlDbType.NVarChar,200),
					new SqlParameter("@PostCode", SqlDbType.NVarChar,50),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@UpdateTime", SqlDbType.DateTime),
					new SqlParameter("@IsActive", SqlDbType.Int,4),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@PayTime", SqlDbType.DateTime),
					new SqlParameter("@PayWayId", SqlDbType.Int,4),
					new SqlParameter("@DeliveryWayId", SqlDbType.Int,4),
					new SqlParameter("@DeliveryFee", SqlDbType.Money,8),
					new SqlParameter("@CouponFee", SqlDbType.Money,8),
					new SqlParameter("@ProductPrice", SqlDbType.Money,8),
					new SqlParameter("@DeliveryTimeId", SqlDbType.Int,4),
					new SqlParameter("@trade_no", SqlDbType.NVarChar,50),
					new SqlParameter("@OrderId", SqlDbType.Int,4)};
			parameters[0].Value = model.UserId;
			parameters[1].Value = model.Total;
			parameters[2].Value = model.OitemNum;
			parameters[3].Value = model.Name;
			parameters[4].Value = model.sex;
			parameters[5].Value = model.Phone;
			parameters[6].Value = model.Mobile;
			parameters[7].Value = model.Province;
			parameters[8].Value = model.City;
			parameters[9].Value = model.ProvinceName;
			parameters[10].Value = model.CityName;
			parameters[11].Value = model.Address;
			parameters[12].Value = model.PostCode;
			parameters[13].Value = model.CreateTime;
			parameters[14].Value = model.UpdateTime;
			parameters[15].Value = model.IsActive;
			parameters[16].Value = model.Status;
			parameters[17].Value = model.PayTime;
			parameters[18].Value = model.PayWayId;
			parameters[19].Value = model.DeliveryWayId;
			parameters[20].Value = model.DeliveryFee;
			parameters[21].Value = model.CouponFee;
			parameters[22].Value = model.ProductPrice;
			parameters[23].Value = model.DeliveryTimeId;
			parameters[24].Value = model.trade_no;
			parameters[25].Value = model.OrderId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int OrderId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Orders ");
			strSql.Append(" where OrderId=@OrderId");
			SqlParameter[] parameters = {
					new SqlParameter("@OrderId", SqlDbType.Int,4)
			};
			parameters[0].Value = OrderId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string OrderIdlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Orders ");
			strSql.Append(" where OrderId in ("+OrderIdlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.Orders GetModel(int OrderId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 OrderId,UserId,Total,OitemNum,Name,sex,Phone,Mobile,Province,City,ProvinceName,CityName,Address,PostCode,CreateTime,UpdateTime,IsActive,Status,PayTime,PayWayId,DeliveryWayId,DeliveryFee,CouponFee,ProductPrice,DeliveryTimeId,trade_no from Orders ");
			strSql.Append(" where OrderId=@OrderId");
			SqlParameter[] parameters = {
					new SqlParameter("@OrderId", SqlDbType.Int,4)
			};
			parameters[0].Value = OrderId;

			Maticsoft.Model.Orders model=new Maticsoft.Model.Orders();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["OrderId"]!=null && ds.Tables[0].Rows[0]["OrderId"].ToString()!="")
				{
					model.OrderId=int.Parse(ds.Tables[0].Rows[0]["OrderId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["UserId"]!=null && ds.Tables[0].Rows[0]["UserId"].ToString()!="")
				{
					model.UserId=int.Parse(ds.Tables[0].Rows[0]["UserId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Total"]!=null && ds.Tables[0].Rows[0]["Total"].ToString()!="")
				{
					model.Total=decimal.Parse(ds.Tables[0].Rows[0]["Total"].ToString());
				}
				if(ds.Tables[0].Rows[0]["OitemNum"]!=null && ds.Tables[0].Rows[0]["OitemNum"].ToString()!="")
				{
					model.OitemNum=int.Parse(ds.Tables[0].Rows[0]["OitemNum"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Name"]!=null && ds.Tables[0].Rows[0]["Name"].ToString()!="")
				{
					model.Name=ds.Tables[0].Rows[0]["Name"].ToString();
				}
				if(ds.Tables[0].Rows[0]["sex"]!=null && ds.Tables[0].Rows[0]["sex"].ToString()!="")
				{
					model.sex=int.Parse(ds.Tables[0].Rows[0]["sex"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Phone"]!=null && ds.Tables[0].Rows[0]["Phone"].ToString()!="")
				{
					model.Phone=ds.Tables[0].Rows[0]["Phone"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Mobile"]!=null && ds.Tables[0].Rows[0]["Mobile"].ToString()!="")
				{
					model.Mobile=ds.Tables[0].Rows[0]["Mobile"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Province"]!=null && ds.Tables[0].Rows[0]["Province"].ToString()!="")
				{
					model.Province=int.Parse(ds.Tables[0].Rows[0]["Province"].ToString());
				}
				if(ds.Tables[0].Rows[0]["City"]!=null && ds.Tables[0].Rows[0]["City"].ToString()!="")
				{
					model.City=int.Parse(ds.Tables[0].Rows[0]["City"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ProvinceName"]!=null && ds.Tables[0].Rows[0]["ProvinceName"].ToString()!="")
				{
					model.ProvinceName=ds.Tables[0].Rows[0]["ProvinceName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["CityName"]!=null && ds.Tables[0].Rows[0]["CityName"].ToString()!="")
				{
					model.CityName=ds.Tables[0].Rows[0]["CityName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Address"]!=null && ds.Tables[0].Rows[0]["Address"].ToString()!="")
				{
					model.Address=ds.Tables[0].Rows[0]["Address"].ToString();
				}
				if(ds.Tables[0].Rows[0]["PostCode"]!=null && ds.Tables[0].Rows[0]["PostCode"].ToString()!="")
				{
					model.PostCode=ds.Tables[0].Rows[0]["PostCode"].ToString();
				}
				if(ds.Tables[0].Rows[0]["CreateTime"]!=null && ds.Tables[0].Rows[0]["CreateTime"].ToString()!="")
				{
					model.CreateTime=DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["UpdateTime"]!=null && ds.Tables[0].Rows[0]["UpdateTime"].ToString()!="")
				{
					model.UpdateTime=DateTime.Parse(ds.Tables[0].Rows[0]["UpdateTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["IsActive"]!=null && ds.Tables[0].Rows[0]["IsActive"].ToString()!="")
				{
					model.IsActive=int.Parse(ds.Tables[0].Rows[0]["IsActive"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Status"]!=null && ds.Tables[0].Rows[0]["Status"].ToString()!="")
				{
					model.Status=int.Parse(ds.Tables[0].Rows[0]["Status"].ToString());
				}
				if(ds.Tables[0].Rows[0]["PayTime"]!=null && ds.Tables[0].Rows[0]["PayTime"].ToString()!="")
				{
					model.PayTime=DateTime.Parse(ds.Tables[0].Rows[0]["PayTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["PayWayId"]!=null && ds.Tables[0].Rows[0]["PayWayId"].ToString()!="")
				{
					model.PayWayId=int.Parse(ds.Tables[0].Rows[0]["PayWayId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["DeliveryWayId"]!=null && ds.Tables[0].Rows[0]["DeliveryWayId"].ToString()!="")
				{
					model.DeliveryWayId=int.Parse(ds.Tables[0].Rows[0]["DeliveryWayId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["DeliveryFee"]!=null && ds.Tables[0].Rows[0]["DeliveryFee"].ToString()!="")
				{
					model.DeliveryFee=decimal.Parse(ds.Tables[0].Rows[0]["DeliveryFee"].ToString());
				}
				if(ds.Tables[0].Rows[0]["CouponFee"]!=null && ds.Tables[0].Rows[0]["CouponFee"].ToString()!="")
				{
					model.CouponFee=decimal.Parse(ds.Tables[0].Rows[0]["CouponFee"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ProductPrice"]!=null && ds.Tables[0].Rows[0]["ProductPrice"].ToString()!="")
				{
					model.ProductPrice=decimal.Parse(ds.Tables[0].Rows[0]["ProductPrice"].ToString());
				}
				if(ds.Tables[0].Rows[0]["DeliveryTimeId"]!=null && ds.Tables[0].Rows[0]["DeliveryTimeId"].ToString()!="")
				{
					model.DeliveryTimeId=int.Parse(ds.Tables[0].Rows[0]["DeliveryTimeId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["trade_no"]!=null && ds.Tables[0].Rows[0]["trade_no"].ToString()!="")
				{
					model.trade_no=ds.Tables[0].Rows[0]["trade_no"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select OrderId,UserId,Total,OitemNum,Name,sex,Phone,Mobile,Province,City,ProvinceName,CityName,Address,PostCode,CreateTime,UpdateTime,IsActive,Status,PayTime,PayWayId,DeliveryWayId,DeliveryFee,CouponFee,ProductPrice,DeliveryTimeId,trade_no ");
			strSql.Append(" FROM Orders ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" OrderId,UserId,Total,OitemNum,Name,sex,Phone,Mobile,Province,City,ProvinceName,CityName,Address,PostCode,CreateTime,UpdateTime,IsActive,Status,PayTime,PayWayId,DeliveryWayId,DeliveryFee,CouponFee,ProductPrice,DeliveryTimeId,trade_no ");
			strSql.Append(" FROM Orders ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Orders ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.OrderId desc");
			}
			strSql.Append(")AS Row, T.*  from Orders T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Orders";
			parameters[1].Value = "OrderId";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

